package cn.sourcespro.spring.controller;

import cn.sourcespro.spring.crudparams.vo.Vo;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * security
 *
 * @author zhanghaowei
 * @date 2018/7/25
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(Exception.class)
    public String exception(Exception e) {
        e.printStackTrace();
        return "err";
    }

    @ExceptionHandler(AccessDeniedException.class)
    public @ResponseBody Vo badCredentialsException(AccessDeniedException e) {

        return new Vo("没有访问权限", HttpStatus.UNAUTHORIZED);
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public String usernameNotFoundException(UsernameNotFoundException e) {
        e.printStackTrace();
        return "err";
    }

}
